//
//  LCBLayoutButton.m
//  LCBButton
//
//  Created by Administrator on 2017/6/28.
//  Copyright © 2017年 linchuanbin. All rights reserved.
//

#import "LCBLayoutButton.h"
#import "ICHeaderFile.h"

@implementation LCBLayoutButton


-(LCBLayoutButton *)addButton
{
    if(!_addButton)
    {
        _addButton = [[LCBLayoutButton alloc]init];
        _addButton.bounds = CGRectMake(0, 0, 44, 44);
        _addButton.center = CGPointMake(self.imageView.right, self.clearance);
        _addButton.imageSize = CGSizeMake(22, 22);
        _addButton.lcbRelayoutType = LCBRelayoutButtonTypeNoFill;
        [self addSubview:_addButton];
    }
    return _addButton;
}


-(void)setLcbRelayoutType:(LCBRelayoutButtonType)lcbRelayoutType
{
    _lcbRelayoutType = lcbRelayoutType;
    if(lcbRelayoutType != LCBRelayoutButtonTypeNomal)
    {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
}

-(void)setLcbAddType:(LCBaddButtonType)lcbAddType
{
    _lcbAddType = lcbAddType;
    switch (lcbAddType) {
        case LCBaddButtonTypeNomal:{
            [self.addButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_job_nomal"] forState:UIControlStateNormal];
        }break;
        case LCBaddButtonTypeAdd:{
            [self.addButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_job_add"] forState:UIControlStateNormal];
        }break;

        case LCBaddButtonTypeDelete:{
            [self.addButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_job_delete"] forState:UIControlStateNormal];
        }break;
        default:
            [self.addButton removeFromSuperview];
            break;
    }
}

//重写父类方法,改变标题和image的坐标
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    
    CGRect rect;
    switch (self.lcbRelayoutType) {
        case LCBRelayoutButtonTypeNomal:
        {
            return [super imageRectForContentRect:contentRect];
        }break;
        case LCBRelayoutButtonTypeLeft:
        {
            CGFloat x = contentRect.size.width  - self.imageSize.width - self.openEdgeInsets.right-self.clearance;
            CGFloat y =  contentRect.size.height -  self.imageSize.height;
            y = y/2;
            rect = CGRectMake(x,y,self.imageSize.width,self.imageSize.height);
        }break;
        case LCBRelayoutButtonTypeRight:
        {
            CGFloat x =  self.clearance;
            CGFloat y =  contentRect.size.height -  self.imageSize.height;
            y = y/2;
            rect = CGRectMake(x,y,self.imageSize.width,self.imageSize.height);
            
        }break;
        case LCBRelayoutButtonTypeTop:
        {
            CGFloat x =  contentRect.size.width -  self.imageSize.width;
            
            CGFloat y =  contentRect.size.height - self.imageSize.height -self.clearance;
            
            x = x / 2;
            
            rect = CGRectMake(x,y,self.imageSize.width,self.imageSize.height);
            
        }break;
        case LCBRelayoutButtonTypeBottom:
        {
            CGFloat x =  contentRect.size.width -  self.imageSize.width;
            
            CGFloat y =  self.clearance ;
            
            x = x / 2;
            
            rect = CGRectMake(x,y,self.imageSize.width,self.imageSize.height);
            
        }break;
        case LCBRelayoutButtonTypeNo:
        {
            rect = CGRectMake(0.0, 0.0, contentRect.size.width, contentRect.size.height);
            
        }break;
        case LCBRelayoutButtonTypeYse:
        {
            rect = CGRectMake(0.0, 0.0, 0, 0);
        }break;
        case LCBRelayoutButtonTypeNoFill:
        {
            CGFloat x =  contentRect.size.width -  self.imageSize.width;
            CGFloat y =  contentRect.size.height - self.imageSize.height;
            x = x/2;
            y = y/2;
            rect = CGRectMake(x,y, self.imageSize.width, self.imageSize.height);
        }break;
        case LCBRelayoutButtonTypeNoFillLeftclearance:
        {
            CGFloat x =  self.clearance;
            CGFloat y =  contentRect.size.height - self.imageSize.height;
            y = y/2;
            rect = CGRectMake(x,y, self.imageSize.width, self.imageSize.height);
        }break;
        case LCBRelayoutButtonTypeNoFillRightclearance:
        {
            CGFloat x =  contentRect.size.width -  self.imageSize.width - self.clearance;
            CGFloat y =  contentRect.size.height - self.imageSize.height;
            y = y/2;
            rect = CGRectMake(x,y, self.imageSize.width, self.imageSize.height);
        }break;
        case LCBRelayoutButtonTypeRed:
        {
            CGFloat x =  contentRect.size.width -  self.imageSize.width;
            CGFloat y =  contentRect.size.height - self.imageSize.height;
            x = x/2;
            y = y/2;
            rect = CGRectMake(x,y, self.imageSize.width, self.imageSize.height);
        }break;
            
        default:
        {
            return [super imageRectForContentRect:contentRect];
        }break;
    }
    
    return rect;
    
    
}

//重写父类方法,改变标题和title的坐标
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    
    CGRect rect;
    switch (self.lcbRelayoutType) {
        case LCBRelayoutButtonTypeNomal:
        {
            return [super titleRectForContentRect:contentRect];
        }break;
        case LCBRelayoutButtonTypeLeft:
        {
            rect = CGRectMake(self.openEdgeInsets.left,self.openEdgeInsets.top, contentRect.size.width - self.imageSize.width - self.clearance-self.openEdgeInsets.left-self.openEdgeInsets.right, contentRect.size.height-self.openEdgeInsets.top-self.openEdgeInsets.bottom);
        }break;
        case LCBRelayoutButtonTypeRight:
        {
            rect = CGRectMake(self.openEdgeInsets.left+ self.clearance + self.imageSize.width,self.openEdgeInsets.top, contentRect.size.width - self.openEdgeInsets.left - self.openEdgeInsets.right - self.imageSize.width-self.clearance, contentRect.size.height-self.openEdgeInsets.top-self.openEdgeInsets.bottom);
        }break;
        case LCBRelayoutButtonTypeTop:
        {
            
            rect = CGRectMake(self.openEdgeInsets.left,self.openEdgeInsets.top,  contentRect.size.width -self.openEdgeInsets.left-self.openEdgeInsets.right, contentRect.size.height - self.openEdgeInsets.top -self.openEdgeInsets.bottom-self.imageSize.height-self.clearance);
        }break;
        case LCBRelayoutButtonTypeBottom:
        {
            rect = CGRectMake(self.openEdgeInsets.left,self.imageSize.height+self.clearance+self.openEdgeInsets.top , contentRect.size.width  -self.openEdgeInsets.left-self.openEdgeInsets.right,contentRect.size.height - self.openEdgeInsets.top -self.openEdgeInsets.bottom-self.imageSize.height-self.clearance);
        }break;
        case LCBRelayoutButtonTypeNo:
        {
            rect = CGRectMake(0.0, 0.0, 0, 0);
            
        }break;
        case LCBRelayoutButtonTypeYse:
        {
            rect = CGRectMake(self.openEdgeInsets.left,self.openEdgeInsets.top, contentRect.size.width -self.openEdgeInsets.left-self.openEdgeInsets.right, contentRect.size.height- self.openEdgeInsets.top -self.openEdgeInsets.bottom);
        }break;
        case LCBRelayoutButtonTypeNoFill:
        {
            rect = CGRectMake(0.0, 0.0, 0, 0);
        }break;
        case LCBRelayoutButtonTypeNoFillLeftclearance:
        {
            rect = CGRectMake(0.0, 0.0, 0, 0);
        }break;
        case LCBRelayoutButtonTypeNoFillRightclearance:
        {
            rect = CGRectMake(0.0, 0.0, 0, 0);
        }break;
        case LCBRelayoutButtonTypeRed:
        {
            CGFloat x =  contentRect.size.width + self.imageSize.width;
            x = x/2;
            rect = CGRectMake(x,self.openEdgeInsets.top,30,15);
        }break;
            
            
        default:
        {
            return [super titleRectForContentRect:contentRect];
        }break;
    }
    
    return rect;
    
    
}


//-(void)addblockButton:(UIButton *)but
//{
//    if(self.lcbAddType !=LCBaddButtonTypeNomal)
//    {
//        self.lcbAddType = (self.lcbAddType+1)%3;
//        if ([self.delegate respondsToSelector:@selector(addButtonclick:)]) {
//            [self.delegate addButtonclick:but];
//        }
//        
//    }
//}




@end
